1041D - Glider - CodeForces Solution


binary search data structures two pointers *1700

Please click on ads to support us..

Python Code:

import bisect
import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, h = map(int, input().split())
u, v = [], []
for _ in range(n):
    x1, x2 = map(int, input().split())
    u.append(x1)
    v.append(x2)
s = [0]
for i in range(n - 1):
    s.append(s[-1] + u[i + 1] - v[i])
la = v[-1]
ls = s[-1]
ans = 0
for i in range(n):
    x1, x2, si = u[i], v[i], s[i]
    if ls - si < h:
        ans = max(ans, la + h + si - ls - x1)
        break
    j = bisect.bisect_left(s, si + h) - 1
    ans = max(ans, v[j] + h + si - s[j] - x1)
print(ans)

C++ Code:

#include <bits/stdc++.h>
//#include "debug.hpp"
using namespace std;

#define ll long long
#define tc ll tc;cin>>tc ;while(tc--)
#define li cout<<'\n';
#define simba iostream::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define in(arr, n) for(int i = 0 ; i<(n)  ; i++) {cin>>(arr)[i];}
#define out(arr, n) for(int i = 0 ; i<(n)  ; i++) {cout<<(arr)[i]<<" ";} cout<<"\n";
#define sz(x) ((int)(x.size()))
#define rall(v) rbegin(v), rend(v)
#define all(x) x.begin(),x.end()
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;

const int N = 1e5 + 5, INF = INT_MAX, MOD = 1e9 + 7,LOG = 20;
int Xaxis[8]={0,0,-1,1,-1,-1,1,1}; //right,left,up,down
int Yaxis[8]={1,-1,0,0,-1,1,1,-1}; //right,left,up,down
///vector input
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
    for (auto &i: v)
        is >> i;
    return is;
}

//// بسم الله الرحمن الرحيم

void solve() {
    int n, h;
    cin >> n >> h;
    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].first >> v[i].second;
    }
    v.push_back({2e9, 2e9}), n++;
    ll l = 0, r = 0, ans = 0, sum = 0;
    while (r < n) {
        while (v[r].first - v[l].first - sum >= h) {
            sum -= (v[l].second - v[l].first);
            l++;
        }
        sum += v[r].second - v[r].first;
        ans = max(ans, sum), r++;
    }
    cout << ans + h;
}
int main() {
    simba
//    tc{
        solve();
//    }
    return 0;
}



	  	    		   	 		 	   	 	 			 	


Comments

Submit
0 Comments
More Questions

1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation